package com.ydx.Controller;

import com.ydx.entity.User;
import com.ydx.service.UserService;
import com.ydx.util.JsonResponse;
import com.ydx.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    private UserService userService;
    @GetMapping("/UserList")
    public JsonResponse listUser() {
        List<User> users = userService.list();
        System.out.println("来到了用户查询");

        // 构建自定义的响应数据格式
        JsonResponse response = new JsonResponse();
        response.setCode(200);
        response.setMessage("成功!");
        ResponseData result = new ResponseData();
        result.setTotal(users.size());
        result.setList(users);
        response.setResult(result);
        return response;
    }

    @DeleteMapping("/DeleteUser/{userid}")
    public boolean DeleteUser(@PathVariable int userid) {
        System.out.println("删除");
        return userService.removeById(userid);
    }

    @PutMapping("/AddUser")
    public boolean AddUser(@RequestBody User user) {
        System.out.println(user);
        System.out.println("新增");
        return userService.save(user);
    }

    @PostMapping("/UpdateUser")
    public boolean UpdateUser( @RequestBody User user) {
        return userService.updateById(user);
    }
}
